package ncov.service.impl;

import ncov.dao.DepartmentDao;
import ncov.dao.StudentDao;
import ncov.model.*;
import ncov.service.StudentService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author WuChangJian
 * @date 2020/5/30 20:43
 */
@Service
@Transactional
public class StudentServiceImpl implements StudentService {

    private Logger logger = Logger.getLogger(StudentServiceImpl.class);
    @Autowired
    private StudentDao studentDao;
    @Autowired
    private DepartmentDao departmentDao;
    private Integer headcount = 0;

    @Override
    public Student getStudent(String stuId) {
        return studentDao.queryById(stuId);
    }

    @Override
    public void addStudents(List<Student> students, Staff staff, Integer gradeId) {
        if (students != null && students.size() > 0) {
            for (Student student : students) {
                student.setGradeId(gradeId);
            }
            studentDao.batchInsert(students);
            headcount += students.size();
        }
    }

    @Override
    public List<Student> selectPage(PageModel pageModel) {
        // 为空的情况下，默认从索引0开始检索10条记录
        if (pageModel.getCurrentPage() == null) {
            pageModel.setCurrentPage(0);
        }
        if (pageModel.getCount() == null) {
            pageModel.setCount(10);
        }
        return studentDao.queryAllByLimit(pageModel);
    }

    /**
     * 查询指定班级下的所有学生
     * @param gradeId
     * @return
     */
    @Override
    public List<Student> getStudentInClass(Integer gradeId) {
        if (gradeId != null) {
            Student student = new Student();
            student.setGradeId(gradeId);
            return studentDao.queryAll(student);
        }
        return null;
    }

    @Override
    public int clearReportRecord(String stuId) {
        Student student = studentDao.queryById(stuId);
        student.setReportRecord(null);
        studentDao.update(student);
        Department department = departmentDao.queryByName(student.getDepartment());
        department.setSubmittedCount(department.getSubmittedCount() - 1);
        for (Grade grade : department.getGrades()) {
            if (student.getGradeId().equals(grade.getGradeId())) {
                grade.setSubmittedCount(grade.getSubmittedCount() - 1);
            }
        }
        return 0;
    }

    @Override
    public Student update(Student student) {
        if (student.getStuId() != null && student.getStuId().length() > 0) {
            studentDao.update(student);
            return student;
        } else {
            Department department = departmentDao.queryByName(student.getDepartment());
            for (Grade grade : department.getGrades()) {
                if (grade.getGradeId().equals(student.getGradeId())) {
                    grade.setHeadcount(grade.getHeadcount() - 1);
                    department.setHeadcount(department.getHeadcount() - 1);
                    Student stu = studentDao.queryById(student.getId());
                    if (stu.getReportRecord() != null) {
                        grade.setSubmittedCount(grade.getSubmittedCount() - 1);
                        department.setSubmittedCount(department.getSubmittedCount() - 1);
                    }
                }
            }
            studentDao.deleteById(student.getId());
        }
        return null;
    }

    @Override
    public void doAfter(Integer gradeId, Integer deptId) {
        Department department = departmentDao.queryById(deptId);
        department.setHeadcount(department.getHeadcount() + headcount);
        for (Grade grade : department.getGrades()) {
            if (gradeId.equals(grade.getGradeId())) {
                grade.setHeadcount(headcount);
            }
        }
        headcount = 0;// 尽量不要修改成员变量的值！！！
    }

}
